//
//  ScrollPageView.m
//  轮播图
//
//  Created by xalo on 16/2/26.
//  Copyright © 2016年 王陕. All rights reserved.
//

#import "ScrollPageView.h"
#import "NewsModel.h"
#import <UIImageView+WebCache.h>
#import <Reachability.h>

@interface ScrollPageView ()<UIScrollViewDelegate>

@property (nonatomic,assign) NSInteger count;
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,assign) NSInteger index;


@end

@implementation ScrollPageView

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray *)imagesArray modelsArray:(NSArray *)array {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        CGFloat width = frame.size.width;
        CGFloat height = width*0.5;
        _count = imagesArray.count + 1;
        
        //设置scrollView
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        _scrollView.contentSize = CGSizeMake(width * _count, height);
        _scrollView.contentOffset = CGPointMake(width, 0);
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        
        
        
        [self addSubview:_label];
        
        for (int i = 0; i < _count; i++) {
            
            NSInteger current = (i == _count -1)? 0:i;
            NSString *urlString = imagesArray[current];
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.frame = CGRectMake(i * width, 0, width, height);
            
            extern BOOL isWiFiMode;
            Reachability *ablity = [Reachability reachabilityForInternetConnection];
            if ([ablity isReachableViaWiFi] || ([ablity isReachable] && !isWiFiMode) || ![ablity isReachable]) {
                
                [imageView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"background"]];
            }
            
            imageView.userInteractionEnabled = YES;
            imageView.tag = current;
            
            
            // 设置每张图片的label
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, height - 25, width, 25)];
            NewsModel *model = array[current];
            label.text = model.title;
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor blackColor];
            label.alpha = 0.6;
            [imageView addSubview:label];
            
            
            //给imageView添加手势
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailAction:)];
            [imageView addGestureRecognizer:tap];
            
            
            [_scrollView addSubview:imageView];
        }
        [self addSubview:_scrollView];
        
        
        
        //设置pagesControl
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(width - 70, height - 20, 60 , 20)];
        _pageControl.numberOfPages = _count - 1;
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        [_pageControl addTarget:self action:@selector(changeOffset:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_pageControl];
        
        [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];
    }
    
    return self;
}

// 改变页面偏移
- (void)changeOffset:(UIPageControl *)pageControl {
    
    NSInteger currentPage = pageControl.currentPage;
    CGFloat offset = _scrollView.bounds.size.width * (currentPage + 1);
    [_scrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
}

// 自动轮播
- (void)autoPlay {
    
    CGFloat offset = _scrollView.contentOffset.x;
    CGFloat width = _scrollView.bounds.size.width;
    
    if (offset > width*(_count - 1)) {
        [_scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
    }else{
        [_scrollView setContentOffset:CGPointMake(offset + width, 0) animated:YES];
    }
}

#pragma mark - UIScrollView delegate

// 滚动视图时,修改pageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat offset = scrollView.contentOffset.x;
    CGFloat width = scrollView.bounds.size.width;
    NSInteger number = offset/width;
    _pageControl.currentPage = (number == 0 ? _pageControl.numberOfPages : number - 1);
}

// 滚动视图时,修改视图内容
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat width = scrollView.bounds.size.width;
    CGFloat offset = scrollView.contentOffset.x;
    
    if (offset < 0) {
        [scrollView setContentOffset:CGPointMake(width * (_count - 1), 0) animated:NO];
    }
    if (offset > width*(_count - 1)) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
}

// 动画结束后,修改pageControl
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    CGFloat offset = scrollView.contentOffset.x;
    CGFloat width = scrollView.bounds.size.width;
    NSInteger number = offset/width;
    _pageControl.currentPage = (number == 0 ? _pageControl.numberOfPages : number - 1);
}

// 轮播图点击方法
- (void)detailAction:(UITapGestureRecognizer *)tap {
    
    NSInteger index = (self.pageControl.currentPage == self.count-2)?0:self.pageControl.currentPage+1;
    
    [self.delegate pushDetailPagesWithCurrentPages:index];
    
    
}




@end
